/**
 * Copyright &copy; 2017-2020 <a href="http://www.fosung.com/">FosungFrame</a> All rights reserved.
 */
package com.fosung.frame.tsdemo.web.meeting;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.fosung.frame.common.config.Global;
import com.fosung.frame.common.persistence.Page;
import com.fosung.frame.common.utils.StringUtils;
import com.fosung.frame.common.web.BaseController;
import com.fosung.frame.modules.sys.utils.UserUtils;
import com.fosung.frame.tsdemo.entity.meeting.Meeting;
import com.fosung.frame.tsdemo.service.meeting.MeetingService;

/**
 * 党建热点Controller
 * @author zhaofj
 * @version 2017-07-06
 */
@Controller
@RequestMapping(value = "${adminPath}/tsdemo/meeting/meeting")
public class MeetingController extends BaseController {

	@Autowired
	private MeetingService meetingService;
	
	@ModelAttribute
	public Meeting get(@RequestParam(required=false) String id) {
		Meeting entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = meetingService.get(id);
		}
		if (entity == null){
			entity = new Meeting();
		}
		return entity;
	}
	
	@RequiresPermissions("tsdemo:meeting:meeting:view")
	@RequestMapping(value = {"list", ""})
	public String list(Meeting meeting, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Meeting> page = meetingService.findPage(new Page<Meeting>(request, response), meeting); 
		model.addAttribute("page", page);
		return "frame/tsdemo/meeting/meetingList";
	}

	@RequiresPermissions("tsdemo:meeting:meeting:view")
	@RequestMapping(value = "form")
	public String form(Meeting meeting, Model model) {
		//行业
		String modelIds=meeting.getModuleIds();
		if(modelIds!=null&&!modelIds.equals("")){
			List<String> result = Arrays.asList(modelIds.split(",")); 
			meeting.setModelIdsList(result);
		}
		//层级
		String levelIds=meeting.getLevelIds();
		if(levelIds!=null&&!levelIds.equals("")){
			List<String> result = Arrays.asList(levelIds.split(",")); 
			meeting.setLevelIdsList(result);
		}
		model.addAttribute("meeting", meeting);
		return "frame/tsdemo/meeting/meetingForm";
	}

	@RequiresPermissions("tsdemo:meeting:meeting:edit")
	@RequestMapping(value = "save")
	public String save(Meeting meeting, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, meeting)){
			return form(meeting, model);
		}
		//行业
		List<String> modelIdsList=meeting.getModelIdsList();
		if(modelIdsList.size()!=0){
			String str = StringUtils.join(modelIdsList.toArray(), ","); 
			meeting.setModuleIds(str);
		}
		//层级
		List<String> levelIdsList=meeting.getLevelIdsList();
		if(levelIdsList.size()!=0){
			String str = StringUtils.join(levelIdsList.toArray(), ","); 
			meeting.setLevelIds(str);
		}
		if (meeting.getImage()!=null){
			meeting.setImage(StringEscapeUtils.unescapeHtml4(
					meeting.getImage()));
		}
		if (meeting.getContent()!=null){
			meeting.setContent(StringEscapeUtils.unescapeHtml4(
					meeting.getContent()));
		}
		meeting.setCreateuser(UserUtils.getUser());
		meeting.setUpdateuser(UserUtils.getUser());
		//党建栏目id
		meeting.setCategoryId("b17acb79121e4dccaea04035182170dd");
		meeting.setUpdateDate(new Date());
		meetingService.save(meeting);
		addMessage(redirectAttributes, "保存党建热点成功");
		return "redirect:"+Global.getAdminPath()+"/tsdemo/meeting/meeting/?repage";
	}
	
	@RequiresPermissions("tsdemo:meeting:meeting:edit")
	@RequestMapping(value = "delete")
	public String delete(Meeting meeting, RedirectAttributes redirectAttributes) {
		meetingService.delete(meeting);
		addMessage(redirectAttributes, "删除党建热点成功");
		return "redirect:"+Global.getAdminPath()+"/tsdemo/meeting/meeting/?repage";
	}

}